﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Services;
using EasyLibrary.AR.Services;

namespace EasyFrota_Mobile
{
    /// <summary>
    /// Summary description for GpsUpdate
    /// </summary>

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class GpsUpdate : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");
            context.Response.ContentType = "application/json";
            

            string param = context.Request.Params["idActual"];
       
            int id=-1;
            if (param != null)
            {
                id = Convert.ToInt32(param);
            }

            

            var buffer = new List<string>();

            
            var coords = Gps.GetLocalizacaoActual(id);
         
            int i = 0;
            foreach (var coord in coords)
            {

              // var info = infos[i];
               // i++;
               // GpsSpeedCollection speeds = null;
               // GpsFuelLevelCollection fuelLevel = null;
                //if (coord.VehicleId == vIDSelected)
                //{
                //    speeds = Gps.GetSpeedFromTimeInterval(vIDSelected, DateTime.Now, new TimeSpan(0, -2, 0));
                //    fuelLevel = Gps.GetFuelLevelFromTimeInterval(vIDSelected, DateTime.Now, new TimeSpan(0, -2, 0));
                //}
                //buffer.Add(new JsonEncoder(coord, speeds).ToString());
                
                buffer.Add(new JsonEncoder(coord).ToString());
            }
            var s = "[" + string.Join(",", buffer.ToArray()) + "]";
            context.Response.Write(s);
            }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}